package com.wlcorp.forcazamis.web.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wlcorp.forcazamis.sys.util.SessionValue;

/**
 * 版权所有：恒生电子股份有限公司
 * 
 * @包名 : com.hs.cis.web.filter
 * @文件名 : CheckSessionFilter.java
 * @类名 : CheckSessionFilter
 * @功能描述:
 * @创建者 ：chenzy
 * @创建日期: Aug 30, 2006
 * @修订记录：(日期 修订人 修订内容)
 * 
 */
public class CheckSessionFilter implements Filter {
	private FilterConfig filterConfig;

	protected String loginPage = null;

	protected String ignoreURL = null;

	protected ArrayList<String> alIgnoreURL = new ArrayList<String>();

	protected ArrayList<String> alCheckURL = new ArrayList<String>();

	protected static Log log = LogFactory.getLog(CheckSessionFilter.class);

	public void init(FilterConfig filterConfig) {
		this.filterConfig = filterConfig;
		this.loginPage = filterConfig.getInitParameter("loginPage");
		this.ignoreURL = filterConfig.getInitParameter("ignoreURL");
		StringTokenizer tokenizer = new StringTokenizer(ignoreURL, ",");
		while (tokenizer.hasMoreElements()) {
			String s = (String) tokenizer.nextElement();
			alIgnoreURL.add(s);
		}

	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain filterChain) {
		
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		//SessionValue.getInstance(httpRequest).setBackUrl(SessionValue.getWebUrl(httpRequest));
		String url = httpRequest.getRequestURI().toString();
		if (isIgnoreURL(url)) {
			try {
				filterChain.doFilter(request, response);
				return;
			} catch (ServletException sx) {
				filterConfig.getServletContext().log(sx.getMessage());
			} catch (IOException iox) {
				filterConfig.getServletContext().log(iox.getMessage());
			}
		}
		if (SessionValue.getInstance(httpRequest).getStaff() == null) {
			try {
				this.filterConfig.getServletContext().getRequestDispatcher(loginPage).forward(request, response);
				return;
			} catch (ServletException sx) {
				filterConfig.getServletContext().log(sx.getMessage());
			} catch (IOException iox) {
				filterConfig.getServletContext().log(iox.getMessage());
			}
		}
		try {
			filterChain.doFilter(request, response);
		} catch (ServletException sx) {
			sx.printStackTrace();
			filterConfig.getServletContext().log(sx.getMessage());
		} catch (IOException iox) {
			filterConfig.getServletContext().log(iox.getMessage());
		}
	}

	public boolean isIgnoreURL(String url) {
		for (int i = 0; i < this.alIgnoreURL.size(); i++) {
			if (url.indexOf(alIgnoreURL.get(i).toString().trim()) != -1) {
				return true;
			}
		}
		return false;
	}

	public boolean isCheckURL(String url) {
		for (int i = 0; i < this.alCheckURL.size(); i++) {
			if (url.indexOf(alCheckURL.get(i).toString().trim()) != -1) {
				return true;
			}
		}
		return false;
	}

	public void destroy() {
		this.loginPage = null;
		this.filterConfig = null;
		this.ignoreURL = null;
		this.alIgnoreURL = null;
		this.alCheckURL = null;
	}
}
